package demo1;

public class 数组反转 {
    //找规律反转
    public static void main(String[] args) {
        /*
        方法1：交换法
        5/2 = (int)2 ,交换2次
        6/2 = (int)3,交换3次
        交换的次数为数组长度/2
        注意数组越界的边界
         */
        int arr[] = {1,2,3,4,5};
        for (int i = 0; i < arr.length/2; i++) {
            int temp = arr[i];
            arr[i] = arr[arr.length-1-i];
            arr[arr.length-1-i] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+"\t");
        }
        System.out.println();
        System.out.println("方法2");
        /*
        方法2，逆序法
         */
        int arr1[] = {1,2,3,4,5};
        int arr2[] = {1,2,3,4,5};
        for (int i = 0; i < arr1.length; i++) {
            arr1[i]= arr2[arr1.length-1-i];
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr1[i]+"\t");
        }



    }
}
